package jp.co.mclnet.util;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class NonakaCalc {

    public static <E extends Serializable> E cln(E obj) {
        Class<? extends Serializable> clazz = obj.getClass();
        Serializable instance = null;
        try {
            instance = clazz.newInstance();
        } catch (InstantiationException e) {
            throw new IllegalArgumentException(e);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(e);
        }
        @SuppressWarnings("unchecked")
        E result = (E) instance;
        return result;
    }

    public static Object get(Map<Object, Object> zMap, int x, int y, int z) {
        @SuppressWarnings("unchecked")
        Map<Object, Object> yMap = (Map<Object, Object>) zMap.get(z);
        if (yMap == null) {
            yMap = new LinkedHashMap<Object, Object>();
            zMap.put(z, yMap);
        }

        @SuppressWarnings("unchecked")
        Map<Object, Object> xMap = (Map<Object, Object>) yMap.get(y);
        if (xMap == null) {
            xMap = new LinkedHashMap<Object, Object>();
            zMap.put(y, xMap);
        }

        return xMap.get(x);
    }

    protected static void hashikoba() {
        Map<Object, Object> map = new HashMap<Object, Object>();
        {
            int x = 6;
            int y = 1;
            int z = 2;
            Object value = "table=personal";
            put(map, x, y, z, value);
            System.out.println(get(map, x, y, z));
        }
        {
            int x = 6;
            int y = 2;
            int z = 2;
            Object value = "personal_id";
            put(map, x, y, z, value);
            System.out.println(get(map, x, y, z));
        }
    }

    public static void main(String[] args) {
        shimanuki();
        hashikoba();
    }

    public static void put(Map<Object, Object> zMap, int x, int y, int z,
            Object value) {
        @SuppressWarnings("unchecked")
        Map<Object, Object> yMap = (Map<Object, Object>) zMap.get(z);
        if (yMap == null) {
            yMap = new LinkedHashMap<Object, Object>();
            zMap.put(z, yMap);
        }

        @SuppressWarnings("unchecked")
        Map<Object, Object> xMap = (Map<Object, Object>) yMap.get(y);
        if (xMap == null) {
            xMap = new LinkedHashMap<Object, Object>();
            yMap.put(y, xMap);
        }

        xMap.put(x, value);
    }

    protected static void shimanuki() {
        LinkedHashMap<String, Object> bean = new LinkedHashMap<String, Object>();
        LinkedHashMap<String, Object> c = cln(bean);
        System.out.println(bean.equals(c));
    }
}
